/**
 *
 * 加解密方法函数
 * @name aesEncrypt ：  aes加密
 * @name aesDecrypt ：  aes解密
 *
 * @name rc4Encrypt ：  rc4加密
 * @name rc4Decrypt ：  rc4解密
 *
 * @name encrypted ：  res加密
 * @name decrypted ：  res解密
 *
 * @name md5 ：  md5加密
 *
 * */
import CryptoJS from 'crypto-js'
import $md5 from 'js-md5'
import {JSEncrypt} from 'jsencrypt'

import {objKeySort} from './utils'

// aes加密   对称加密
export const aesEncrypt = (data, keyStr, ivStr) => {
  keyStr = keyStr || '268ec48359d9cd64'
  ivStr = ivStr || '1234567890123456'
  let word = JSON.stringify(objKeySort(data))
  let key = CryptoJS.enc.Utf8.parse(keyStr)
  let iv = CryptoJS.enc.Utf8.parse(ivStr)
  let encrypted = CryptoJS.AES.encrypt(word, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
}

// aes解密
export const aesDecrypt = (word, keyStr, ivStr) => {
  keyStr = keyStr || '268ec48359d9cd64'
  ivStr = ivStr || '1234567890123456'
  let key = CryptoJS.enc.Utf8.parse(keyStr)
  let iv = CryptoJS.enc.Utf8.parse(ivStr)
  let baseResult = CryptoJS.enc.Base64.parse(word)
  let ciphertext = CryptoJS.enc.Base64.stringify(baseResult)
  let decrypt = CryptoJS.AES.decrypt(ciphertext, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  return CryptoJS.enc.Utf8.stringify(decrypt)
}

/*
// rc4加密
export function rc4Encrypt (word, keyStr) {
  let encrypt = CryptoJS.RC4.encrypt(word, keyStr)
  return CryptoJS.enc.Base64.stringify(encrypt.ciphertext)
}

// rc4解密
export function rc4Decrypt (word, keyStr) {
  let decrypt = CryptoJS.RC4.decrypt(word, keyStr)
  return CryptoJS.enc.Utf8.stringify(decrypt)
}
*/

// rsa加解密    非对称加密
// 加密
let encrypted = new JSEncrypt()
// 解密
let decrypted = new JSEncrypt()

// 公钥加密
encrypted.setPublicKey(`-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8ZudxB7lO7KRqJFGOPO1Y0Tbw
dKd4r5Ci2ow7JU93R4B8swUl/Cx6t7cnalIhz1ypl8SI5L0mMe0JVIBaFu3rFnWZ
dBU0i/+7ki68XshDGE6NM2XUUmNJFbn2BlgjNmIeY/xKuVuKPDrjcZLRPBwFeOH8
JZlKKZv8vgQTrQ+7+QIDAQAB
-----END PUBLIC KEY-----`)
// 私钥解密
decrypted.setPrivateKey(`-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANfhcynM3KED4yFK
nzTbaLfx0WxPphpYggybBAQHS85wfm6I3mS+IqSLyY0BR3sxSD+mnBFhlE2ochHW
kS1J0lexglSCejcQZO90U+9uVi2Lc2/MPik09iz9UVa/nKfLP+iUQCxEuzvEsdEm
RCSKawjgmrQn6/fHCuxU4TDPIf9pAgMBAAECgYAN8ZDnrUS+3apo9xHrbxSHdQYK
0eYvo3Ha8JRE0lfSIK/54btDlIhWSnUdN2zvhxJckh1Yfg3/KhccEdjUrQT56K2e
Vkz3Oj01OM7IW9NNf0Q6Y3ofrcdZKcioT8Fu4QCss6F+h8xgkjOcvEG80JlY/Q0t
pwaUDncOVpxkxcUBAQJBAOz0/sWd73Rjo63iaFQAnZBYvC5yAXIYsD2ybsAxF6Eb
A4gxNJdaVGYe6DXxlZ+AiB1EG7yde5ElXuQ2JSuRRiECQQDpOtfUPzBAt/hBVTbf
kiQdl8oSPt8DVQ3ixNHWjZLMnOY8+QEnjE3X4UrMfcuOUMt9HTht9CCVvwpYflRi
FQBJAkA8cFD7fskw2qlJFNKfeni19Kwq/JE51Oo/EmAFTYBFa2p/Nm2/nR6h3YhF
GhWb1WDidhjNBy4ZW3rUoqrgbfGBAkEA3qOWXn1HcbBsoulWrYtTQJCijFJKD2NF
EyFxco/I/9wTCUdmK7uKn7bn8VdwVlWYOsIHGSc/Z9Nra9iEcvC7SQJBAJ2KZY6E
mNGrt8P0tRCW/gCIYg9T/hKhJ96j6UDQer4DTjt884mcIcl+of9Lq1sUzEM2rdzX
jN70M2fTs39ItaI=
-----END PRIVATE KEY-----`)

// 私钥加密
/*
encrypted.setPublicKey(`-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8ZudxB7lO7KRqJFGOPO1Y0Tbw
dKd4r5Ci2ow7JU93R4B8swUl/Cx6t7cnalIhz1ypl8SI5L0mMe0JVIBaFu3rFnWZ
dBU0i/+7ki68XshDGE6NM2XUUmNJFbn2BlgjNmIeY/xKuVuKPDrjcZLRPBwFeOH8
JZlKKZv8vgQTrQ+7+QIDAQAB
-----END PUBLIC KEY-----`)
// 公钥解密
decrypted.setPrivateKey(`-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALxm53EHuU7spGok
UY487VjRNvB0p3ivkKLajDslT3dHgHyzBSX8LHq3tydqUiHPXKmXxIjkvSYx7QlU
gFoW7esWdZl0FTSL/7uSLrxeyEMYTo0zZdRSY0kVufYGWCM2Yh5j/Eq5W4o8OuNx
ktE8HAV44fwlmUopm/y+BBOtD7v5AgMBAAECgYBaS6+NGriTvRMn0/RKiaTvtoXw
RNinHO3jP3+KMV2eu2Q3X82N4QioYDil6W0nyyyME7McMRT+o1sJ57gEdYzdFp7H
scnxjYrSp7V0o3CBM9dNXJHslRcq3c9PrtSSrzU7GYOQHP0UABt6Vb+ImFKVKofX
olRZmQ8Tgdej8GtIqQJBAOR8D/16jd1FhjdkxL4/ozRoDNks/Y33gv8uaIKZvcng
kbFaajUpQexucsMDLjAUWIcf2ibDadrEr/KRU6v/qncCQQDTFyLc8R1ScmtSR3Zb
Q2xtNkhgnh8AxIOnyBCFWvuvHFp0FO3lBzmEeEsM3FxaS6tWfMn4m9jMWYU4Prhw
sfkPAkAj397a280+etYSCH5jqLZFWjj6OHgvPDeTPYQnTWBhPCNkB2A5rJqvNsBK
Iku0PN2RiczE3VXXhZ5ipbLPhLsNAkEAnsT/Huln4ZRX7nn7pqcuzXIPvEQSE5S7
ZZ4P9w240jvkOP1o9WOaum10HYc9G1lg1NtlWk12IjWPAwAB4BmstwJBAKUQl9Z/
ErV4NwB/JI9TAqh7WoYuI5uWK1nxJ2A+8LVYL25IMxoJnyCV5GmeldR2WvKay+QY
94mF11Do3kCZXFc=
-----END PRIVATE KEY-----`)
*/

export {
  decrypted,
  encrypted
}

// md5加密拼装
export function md5 (word, keyStr) {
  return $md5($md5(keyStr + word + keyStr))
}
